package com.ruoyi.mateDevice.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateCheck.domain.MateUserDevice;
import com.ruoyi.mateDevice.domain.DeviceCalibration;
import com.ruoyi.mateDevice.domain.DeviceCalibrationModel;
import com.ruoyi.mateDevice.domain.DeviceClean;
import com.ruoyi.mateDevice.domain.DevicePreserve;
import com.ruoyi.mateDevice.domain.DeviceRepair;
import com.ruoyi.mateDevice.domain.DeviceRepairModel;
import com.ruoyi.mateDevice.domain.MateDevice;
import com.ruoyi.mateDevice.domain.MateDeviceModel;
import com.ruoyi.mateDevice.mapper.MateDeviceMapper;
import com.ruoyi.mateDevice.service.MateDeviceService;
import com.ruoyi.mateRequistion.domain.MateRequistionModel;
import com.ruoyi.material.domain.MaterialProduct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MateDeviceServiceImpl implements MateDeviceService {

    @Autowired
    MateDeviceMapper mateDeviceMapper;


    @Override
    public int insertMateDevice(MateDevice mateDevice) {
        mateDevice.setDeptId(SecurityUtils.getDeptId());
        int a = mateDeviceMapper.insertMateDevice(mateDevice);
        return a;
    }

    @Override
    public List<MateDevice> selectMateDeviceList(MateDevice mateDevice) {
        return mateDeviceMapper.selectMateDeviceList(mateDevice);

    }

    @Override
    public int updatemateDevice(MateDevice mateDevice) {
        SysUser sysUser = SecurityUtils.getLoginUser().getUser();
        Long mtdeviceId = mateDevice.getMtdeviceId();
        mateDevice.setDeptId(sysUser.getDeptId());
        mateDeviceMapper.deleteMateDevice(mtdeviceId);
        int a = mateDeviceMapper.insertMateDevice(mateDevice);
        return a;
    }

    @Override
    public List<MaterialProduct> selectProduct(Long companyId) {
        return mateDeviceMapper.selectProduct(companyId);
    }

    @Override
    public int insertUserDevice(MateUserDevice mateUserDevice) {
        if (mateUserDevice != null) {
            List<MateUserDevice> mateUserDevices = mateDeviceMapper.selectMateUserDeviceList(mateUserDevice);
            for (MateUserDevice mateUserDevice1 : mateUserDevices) {
                mateUserDevice1.setDeviceStatus("正常");
                mateDeviceMapper.insertUserDevice(mateUserDevice1);
            }
        }
        return 0;
    }

    @Override
    public List<MateUserDevice> selectUserDevice(MateUserDevice mateUserDevice) {
        List<MateUserDevice> list = mateDeviceMapper.selectUserDeviceList(mateUserDevice);
        return list;
    }

    @Override
    public MateUserDevice selectUserEdit(Long userDeviceId) {
        return mateDeviceMapper.selectUserEdit(userDeviceId);
    }

    @Override
    public int updateUserDevice(MateUserDevice mateUserDevice) {
        int a = mateDeviceMapper.updateUserDevice(mateUserDevice);
        return a;
    }

    @Override
    public int addRepair(DeviceRepair deviceRepair) {
        deviceRepair.setDeptId(SecurityUtils.getDeptId());
        int a = mateDeviceMapper.addRepair(deviceRepair);

        return a;
    }

    @Override
    public List<DeviceRepair> selectRepairList(DeviceRepair deviceRepair) {
        return mateDeviceMapper.selectRepairList(deviceRepair);
    }

    @Override
    public List<DevicePreserve> perserveList(DevicePreserve devicePreserve) {
        return mateDeviceMapper.perserveList(devicePreserve);
    }

    @Override
    public int addPreserve(DevicePreserve devicePreserve) {
        return mateDeviceMapper.addPreserve(devicePreserve);
    }

    @Override
    public int addDeviceClean(DeviceClean deviceClean) {
        return mateDeviceMapper.addDeviceClean(deviceClean);
    }

    @Override
    public List<DeviceClean> selectCleanList(DeviceClean deviceClean) {
        return mateDeviceMapper.selectCleanList(deviceClean);
    }

    @Override
    public int addDeviceCalibration(DeviceCalibration deviceCalibration) {
        int num = mateDeviceMapper.addDeviceCalibration(deviceCalibration);
        if (deviceCalibration.getCalibrationModels().size() != 0) {
            for (DeviceCalibrationModel deviceCalibrationModel : deviceCalibration.getCalibrationModels()) {
                MateDevice mateDevice = mateDeviceMapper.selectMateDevice(deviceCalibrationModel.getMtdeviceId());
                deviceCalibrationModel.setCalibrationId(deviceCalibration.getCalibrationId());
                deviceCalibrationModel.setMtdeviceId(mateDevice.getMtdeviceId());
                deviceCalibrationModel.setDeviceDept(mateDevice.getDeviceDept());
                deviceCalibrationModel.setDeviceType(mateDevice.getDeviceType());
                deviceCalibrationModel.setDeviceName(mateDevice.getDeviceName());
                deviceCalibrationModel.setDeviceNum(mateDevice.getDeviceNum());
                mateDeviceMapper.addDeviceCalibrationModel(deviceCalibrationModel);
            }
        }

        return num;
    }
}
